﻿
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;


namespace UdpApplication{
    public class ServerBuilder{
        private const int ListenPort = 11000;
        public static void Build(Control status){
            bool done = false;
            UdpClient listener = new UdpClient(ListenPort);
            IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, ListenPort);
            try {
                while (!done) {
                    status.Text += "Waiting for broadcast" + Environment.NewLine;
                    byte[] bytes = listener.Receive(ref groupEP);
                    status.Text += "Received broadcast from " + groupEP + " :" + Environment.NewLine;
                    status.Text +=  Encoding.ASCII.GetString(bytes, 0, bytes.Length) + Environment.NewLine;
                }
            }
            catch (Exception e) {
                status.Text = e.StackTrace;
            }
            finally {
                listener.Close();
            }
        }
    }
}